﻿''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
Public Class AdvancedDatagrid
    Inherits Windows.Controls.DataGrid

    Private _ShowFilterCommand As UICommand
    Private _defaultfilter As String

    Sub New()
        MyBase.New()

        ClipboardCopyMode = Windows.Controls.DataGridClipboardCopyMode.IncludeHeader
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property ShowFilterCommand As UICommand
        Get
            If _ShowFilterCommand Is Nothing Then
                _ShowFilterCommand = New UICommand(AddressOf ShowFilter)
            End If
            Return _ShowFilterCommand
        End Get
    End Property

    Private Sub ShowFilter(parameter As Object)
        Dim filter = New ViewModelTableViewerFilter(Me, parameter)
        If columndata.ContainsKey(parameter) Then
            columndata(parameter) = filter
        Else
            columndata.Add(parameter, filter)
        End If
        PMTModule.UIService.ShowMessage(filter, WindowTitle:=parameter, ShowCloseButton:=False)
    End Sub

    Private columndata As New Dictionary(Of String, ViewModelTableViewerFilter)

    Friend Function UpdateFilter() As String
        Dim q = From elt In columndata Select GetFilter(elt.Value)

        Dim q2 = From elt In q Where Not (String.IsNullOrEmpty(elt)) Select elt

        If q2.Count() = 0 Then Return ""

        Return String.Join(" AND ", q2.ToArray())
    End Function

    Private Function GetFilter(filter As ViewModelTableViewerFilter) As String
        If filter.Selecteds.Count = 0 Or filter.Selecteds.Count = filter.Values.Count Then
            Return ""
        End If

        Dim q = From elt In filter.Selecteds
                  Select "(" & _
                  filter.ColumnName & _
                  " = " & _
                  If(elt.GetType() Is GetType(String), "'" & elt.ToString() & "'", Convert.ToString(elt, System.Globalization.CultureInfo.InvariantCulture)) & ")"

        Return "(" & String.Join(" OR ", q.ToArray()) & ")"

    End Function

    Public Property ShowFilterButton As Boolean
        Get
            Return GetValue(ShowFilterButtonProperty)
        End Get

        Set(ByVal value As Boolean)
            SetValue(ShowFilterButtonProperty, value)
        End Set
    End Property

    Public Shared ReadOnly ShowFilterButtonProperty As Windows.DependencyProperty = _
                           Windows.DependencyProperty.Register("ShowFilterButton", _
                           GetType(Boolean), GetType(AdvancedDatagrid), _
                           New Windows.PropertyMetadata(True))

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property DefaultFilter As String
        Get
            Return GetValue(DefaultFilterProperty)
        End Get

        Set(ByVal value As String)
            SetValue(DefaultFilterProperty, value)
        End Set
    End Property

    Public Shared ReadOnly DefaultFilterProperty As Windows.DependencyProperty = _
                           Windows.DependencyProperty.Register("DefaultFilter", _
                           GetType(String), GetType(AdvancedDatagrid), _
                           New Windows.PropertyMetadata(Nothing))

    Protected Overrides Sub OnAutoGeneratingColumn(e As Windows.Controls.DataGridAutoGeneratingColumnEventArgs)
        '  IO.File.AppendAllText("test.txt", e.PropertyName & ":" & e.PropertyType.ToString() & ":" & (e.Column Is Nothing).ToString() & vbCrLf)


        If e.PropertyType IsNot GetType(String) Then


            Dim col = TryCast(e.Column, Windows.Controls.DataGridTextColumn)

            If col IsNot Nothing Then

                If col.Binding IsNot Nothing Then

                    If e.PropertyName.ToLower().Contains("taux") Or _
                       e.PropertyName.ToLower().Contains("coeff") Or _
                       e.PropertyName.ToLower().Contains("cs") Or _
                       e.PropertyName.ToLower().Contains("quote") Or _
                       e.PropertyName = "Deroule_Prime" Or _
                       e.PropertyName = "Deroule_Reglement_DC" Or _
                       e.PropertyName = "Deroule_Reglement_IT" Then

                        col.Binding.StringFormat = "P2"

                    End If

                    If e.PropertyName.ToLower().Contains("valeur") Or _
                       e.PropertyName = "Primes" Or _
                       e.PropertyName = "Reglement_DC" Or _
                       e.PropertyName = "Reglement_IT" Then

                        col.Binding.StringFormat = "N2"

                    End If


                End If

                Dim style = New Windows.Style()
                style.Setters.Add( _
                    New Windows.Setter(System.Windows.FrameworkElement.HorizontalAlignmentProperty, System.Windows.HorizontalAlignment.Right))
                col.ElementStyle = style


            End If

        End If

        MyBase.OnAutoGeneratingColumn(e)
    End Sub


    Protected Overrides Sub OnCellEditEnding(e As Windows.Controls.DataGridCellEditEndingEventArgs)
        If e.EditAction = Windows.Controls.DataGridEditAction.Commit Then
            Dim txtbox = TryCast(e.EditingElement, Windows.Controls.TextBox)
            If txtbox IsNot Nothing Then
                If txtbox.Text.Contains("%") Then
                    Dim newtxt = txtbox.Text.Trim().Replace("%", "").Trim().Replace(" ", "").Trim()
                    If newtxt.Contains(".") Then
                        txtbox.Text = Convert.ToDouble(newtxt, Globalization.CultureInfo.InvariantCulture) / 100.0
                    Else
                        txtbox.Text = Convert.ToDouble(newtxt, Globalization.CultureInfo.CurrentCulture) / 100.0
                    End If
                End If
            End If
        End If
        MyBase.OnCellEditEnding(e)
    End Sub
End Class
